<?php

class Gallery_EditController extends Crb_Controller
{
	
    public function preDispatch()
    {
        parent::preDispatch();
        $this->view->layout()->setLayout('admin');
        
//        $this->view->headScript()->appendFile('stuff/lightbox/js/prototype.js');
//        $this->view->headScript()->appendFile('stuff/lightbox/js/scriptaculous.js?load=effects,builder');
//        $this->view->headScript()->appendFile('stuff/lightbox/js/lightbox.js');
//        $this->view->headLink()->appendStylesheet('stuff/lightbox/css/lightbox.css');
       
    }
    
	 public function indexAction()
    {
        $Gallery = new Gallery();
        $this->view->Galleries = $Gallery->getList();    
    }
    
    
    public function addAction()
    {
        $Gallery = new Gallery();
        $YID = $Gallery->create();
        $this->_redirect('gallery/edit/edit/id/' . $YID);
    }
    
    public function editAction()
    {
        $Gallery = new Gallery();
        $YID = $this->_request->getParam('id',0);
        $this->view->Gallery = $Gallery->getOne($YID);

        if ($this->_request->isPost())
        {
        	if (isset(    $_FILES['photo']['name'][0]) && !empty($_FILES['photo']['name'][0]))
        	{
        		$NewFileNames   = array();
        		$ImagesPath     = realpath(APPLICATION_PATH .  "/../.." . DIRECTORY_SEPARATOR . 'images');
        		//$ImagesPath     = 'c:/tmp/images';
        		
                $FullImagePath  = $ImagesPath . DIRECTORY_SEPARATOR . 'full';
                $SmallImagePath = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_small';
                $BigImagePath   = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_big';
                    
        		foreach ($_FILES['photo']['name'] as $Key => $File)
        		{
        			
        			if (empty($_FILES['photo']['tmp_name'][$Key])) continue;
        			$NewFileName = md5(time() . $_FILES['photo']['tmp_name'][$Key]) . ".jpg";

        			copy($_FILES['photo']['tmp_name'][$Key],$FullImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        			$NewFileNames[] = $NewFileName;
        			$ImageProc = new Image();
        			$ImageProc->cropResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,221,80,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
//        			$ImageProc->hardResize($SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName,139,80,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        			
//        			$ImageProc->cropResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,578,327,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);

        			$ImageProc->hardResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName, 720, 260, $BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        			
//        			$ImageProc->hardResize($BigImagePath . DIRECTORY_SEPARATOR . $NewFileName,500,400,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        		}
        		$Gallery->insertPictures($YID,$NewFileNames);
        	
        	}
        	else
        	{
        	}
        	
        	
        	$Gallery->save($YID,$_POST);
        	
        	$this->_redirect('gallery/edit');
        }
        
    }
    
    public function deleteAction()
    {
        $Gallery = new Gallery();
        $YID = $this->_request->getParam('id',0);
        $Gallery->delete($YID);
        $this->_redirect('gallery/edit');
            
    }
    
    public function picturedeleteAction()
    {
        $Gallery = new Gallery();
        $PID = $this->_request->getParam('id',0);
        $YID = $this->_request->getParam('yid',0);
        $Gallery->deletePicture($PID);
        $this->_redirect('gallery/edit/edit/id/' . $YID);
            
    }
    
    
    function pictureuploadAction()
    {
        $Gallery = new Gallery();
        
//                Logger::log(print_r($this->_getAllParams(),1));
        
        $ImagesPath     = realpath(ROOT . "/images");
                
        $FullImagePath  = $ImagesPath . DIRECTORY_SEPARATOR . 'full';
        $SmallImagePath = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_small';
        $BigImagePath   = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_big';
    	
    	$YID = $this->_request->getParam('yid',0);
        $Name = basename($_FILES['Filedata']['name']);
        $OldName = $_FILES['Filedata']['tmp_name'];
         $NewFileName = md5(time() . $OldName) . ".jpg";
         copy($OldName,$FullImagePath . DIRECTORY_SEPARATOR . $NewFileName);
         
         $NewFileNames= array($NewFileName);
         $ImageProc = new Image_Resizer();
         
         
         
//         $ImageProc->cropResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,221,80,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
//        			$ImageProc->hardResize($SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName,139,80,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        			
//        			$ImageProc->cropResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,578,327,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);

//        			$ImageProc->hardResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName, 720, 260, $BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
         
         $format = $this->_getParam('format','normal');
         if ($format == "wide")
         {
             $ImageProc->hardResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,240,80,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
             $ImageProc->hardResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,780, 260,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
         }
         else
         {
             $ImageProc->hardResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName, 188, 131, $SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
             $ImageProc->smartResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,900, 800,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
         }
         
         
         
         $Gallery->insertPictures($YID,$NewFileNames);
         exit();
    }
    
    
    
    function ajaxGetImagesAction()
    {
        $this->disableLayout();
        $Gallery = new Gallery();
        $YID = $this->_request->getParam('id',0);
        $this->view->Gallery = $Gallery->getOne($YID);
    }
    
    
    function savePictureDataAction()
    {
        $this->disableLayout();
        $this->setNoRender();
        
        
        $Gallery = new Gallery();
        $PID = $this->_request->getParam('pid',0);
        $Param = $this->_request->getParam('param',0);
        $Value = $this->_request->getParam('value',0);
        $Params[$Param] = $Value;
        
        $Gallery->savePicture($PID,$Params);
    }
	
	
}

